package com.qk.controller;


import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    /**
     * 新增商机
     */
    @PostMapping
    public Result add(@RequestBody Business business) {
        log.info("新增商机: {}" , business);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        business.setStatus(1); // 待分配
        businessService.save(business);
        return Result.success();
    }

    /**
     *  查询商机
     */
    @GetMapping
    public Result list(BusinessDto businessDto) {
        log.info("查询参数: {}" , businessDto);
        PageResult<Business> pageResult = businessService.listBusiness(businessDto);
        return Result.success(pageResult);
    }

    /**
     * 分配商机
     */
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assignBusiness(@PathVariable Integer businessId, @PathVariable Integer userId) {
        log.info("分配商机: {}, {}", businessId, userId);
        businessService.assignBusiness(businessId, userId);
        return Result.success();
    }

    /**
     * 根据id查询商机
     */
    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable Integer id) {
        log.info("查询商机: {}", id);
        Business business = businessService.getBusinessById(id);
        return Result.success(business);
    }

    /**
     * 跟进商机
     */
    @PutMapping
    public Result trackBuisness(@RequestBody Business business) {
        log.info("跟进商机: {}", business);
        businessService.trackBuisness(business);
        return Result.success();
    }

}
